package com.mall.sun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mall.sun.model.dto.BaseChartDTO;
import com.mall.sun.model.po.Permission;
import com.mall.sun.model.po.Role;
import com.mall.sun.model.po.User;
import com.mall.sun.model.po.UserRole;
import com.mall.sun.repository.mapper.RoleMapper;
import com.mall.sun.repository.mapper.UserMapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.sun.repository.mapper.UserRoleMapper;
import com.mall.sun.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Tansty
 * @since 2021-11-30
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserRoleMapper userRoleMapper;
    @Autowired
    RoleMapper roleMapper;
    @Override
    public List<Permission> findPermissionByUsername(String username) {
        return userMapper.findPermissionByUsername(username);
    }

    @Override
    public List<Role> findRoleByUsername(String username) {
        return userMapper.findRoleByUsername(username);
    }

    @Override
    public BaseChartDTO[] userMapCount() {
        return userMapper.userMapCount();
    }

    @Override
    public int register(User user) {
        userMapper.insert(user);
        String guest = "ROLE_GUEST";
        User one = userMapper.selectOne(new QueryWrapper<User>().eq("user_name", user.getUserName()));
        Role role = roleMapper.selectOne(new QueryWrapper<Role>().eq("role_name", guest));
        UserRole userRole = new UserRole()
                .setUserId(one.getId())
                .setRoleId(role.getId());
        return userRoleMapper.insertUserRole(userRole);
    }

}
